<template>
<div class="vue-box" style="display: none;" :style="'display: block;'">
  <div class="c-panel">
    <!-- 参数栏 -->
    <div class="c-title">检索参数</div>
    <el-form @submit.native.prevent class="conditional_filter">
      <sa-item v-model="p.search" type="text" name="模糊搜索" />
      <sa-item v-model="p.status" type="enum" jtype="4" :jv="dims.YES_OR_NO" name="启用状态" :def="{}" :clearable="true" />
      <div class="c-item">
        <el-button type="primary" icon="el-icon-search" @click="p.current = 1; f5()">查询</el-button>
      </div>
    </el-form>
    <!-- ------------- 快捷按钮 ------------- -->
    <sa-item type="fast-btn" show="add,get,delete,export,reset" />
    <!-- ------------- 数据列表 ------------- -->
    <el-table ref="data-table" class="data-table" :data="dataList" border stripe>
      <sa-td type="selection" />
      <sa-td type="text" name="序号" prop="sort" width="80px" />
      <sa-td type="text" name="字典名" prop="name" />
      <sa-td type="text" name="编码" prop="code" />
      <sa-td type="enum" name="启用状态" prop="status" :jv="dims.YES_OR_NO" />
      <sa-td name="操作" fixed="right" width="260px">
        <template slot-scope="s">
          <div class="oper">
            <el-button type="text" @click="addDictItem(s.row)">
                新增子项
            </el-button>

            <el-button type="text" @click="showItems(s.row)">
              子项
            </el-button>

            <el-button type="text" @click="update(s.row)">
              变更
            </el-button>

            <el-button type="text" @click="del(s.row)">
              <span style="color: red;">
                删除
              </span>
            </el-button>
          </div>
        </template>
      </sa-td>
    </el-table>
    <!-- 分页 -->
    <div v-if="dataCount">
      <sa-item type="page" :curr.sync="p.current" :size.sync="p.size" :total="dataCount" @change="f5()" />
    </div>
  </div>
</div>
</template>

<script>
export default {
  name: 'dict-list',
  data() {
    return {
      // 查询参数
      p: {
        current: 1,
        size: 10,
        search: null,
        status: null,
        sortField: null
      },
      dataCount: null,
      // 数据集合
      dataList: [],
      // 缓存字典
      dims: sa.$sys.getCurd(),
    }
  },
  created: function () {
    this.f5();
    // 监听表单动作
    sa.onInputEnter();
  },
  watch: {},
  methods: {
    // 刷新
    f5() {
      sa.ajax('/dict/getList', sa.removeNull(this.p), function (res) {
        if (res.code == 200) {
          // 数据
          this.dataList = res.data;
          // 数据总数
          this.dataCount = res.dataCount;
          this.items = this.dataList[0].items;
          // 刷新表格高度
          sa.f5TableHeight();
        }
      }.bind(this), {
        type: "post"
      });
    },
    // 新增
    add() {
      sa.showModel('添加', () => import('./add'), {
        dims: this.dims
      });
    },
    // 更新
    update(data) {
      data.dims = this.dims
      sa.showModel('变更', () => import('./update'), data);
    },
    // 查看 - 根据选中的
    getBySelect: function (_data) {
      var selection = this.$refs['data-table'].selection;
      if (selection.length === 0) {
        return sa.msg('请至少选择一条数据')
      }
      this.getInfo(selection[0]);
    },
    // 查看详情
    getInfo: function (data) {
      sa.showModel('详情', () => import('./info'), data);
    },
    // 删除
    del(data) {
      sa.confirm('是否删除，此操作不可撤销', function () {
        sa.ajax('/dict/delete?id=' + data.id, function (_res) {
          sa.ok('删除成功');
          this.f5()
        }.bind(this), {
          type: 'get'
        })
      }.bind(this));
    },
    // 批量删除
    deleteByIds() {
      // 获取选中元素的id列表
      let selection = this.$refs['data-table'].selection;
      let ids = sa.getArrayField(selection, 'id');
      if (selection.length == 0) {
        return sa.msg('请至少选择一条数据')
      }
      // 提交删除
      sa.confirm('是否批量删除选中数据？此操作不可撤销', function () {
        sa.ajax('/dict/deleteByIds', {
          ids: ids.join(',')
        }, function (res) {
          sa.ok(res.msg);
          this.f5()
        }.bind(this))
      }.bind(this));
    },

    //子项
    showItems(data) {
      sa.showModel('子项', () => import('./item-info'), data);
    },

    // 新增字典项
    addDictItem(row) {
      // 弹出新增页面
      sa.showModel('添加', () => import('./item-add'), {
        dictId: row.id,
        dims: this.dims
      });
    },
  }
}
</script>

<style scoped>
.dict_item {
  display: flex;
  justify-content: space-between;
}

.oper {
  display: flex;
  justify-content: space-evenly;
}
</style>
